import functools

from flask import session,current_app,g

from info.models import User


def filter1(index):
    if index==0:
        return 'first'
    elif index==1:
        return 'second'
    elif index==2:
        return 'third'
    else:
        return ''




# 把登陆代码封装成装饰器
# 但是会改变被封装函数的函数名变成内层函数的函数名

def login_required(f):
    @functools.wraps(f)
    def wrapper(*args,**kwargs):
        user_id = session.get('user_id')
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 使用应用上下文对象g，来临时存储用户数据，
        # 把查询结果的user对象，赋值给g的属性存储
        g.user = user

        return f(*args,**kwargs)
    # wrapper.__name__=f.__name__
    return wrapper